﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPAMain工序结余锁定工具
{
    public partial class frmMain : Form
    {
        UserInfo ui;

        SqlTools sqt_TP;
        SqlTools sqt_DB;

        public frmMain(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
            this.Text = this.Text + " - " + ui.UserName;
            //sqt = new SqlTools(GeneralTools.GetConnectionString("192.168.1.1", "TPAMain"));
            sqt_TP = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
            sqt_DB = new SqlTools(GeneralTools.GetConnectionString("DefualtDataBase"));
        }

        private void rbtnGDNo_CheckedChanged(object sender, EventArgs e)
        {
            txtGDNo.Enabled = rbtnGDNo.Checked;
            if (rbtnGDNo.Checked)
            {
                rbtnSN.Checked = false;
                rbtnChipNo.Checked = false;
            }
        }

        private void rbtnSN_CheckedChanged(object sender, EventArgs e)
        {
            txtSN.Enabled = rbtnSN.Checked;
            if (rbtnSN.Checked)
            {
                rbtnGDNo.Checked = false;
                rbtnChipNo.Checked = false;
            }
        }

        private void rbtnChipNo_CheckedChanged(object sender, EventArgs e)
        {
            txtChipNo.Enabled = rbtnChipNo.Checked;
            if (rbtnChipNo.Checked)
            {
                rbtnGDNo.Checked = false;
                rbtnSN.Checked = false;
            }
        }

        private void rbtnAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnAll.Checked)
            {
                rbtnNoUnlockOver.Checked = false;
                rbtnUnlockOver.Checked = false;
            }
        }

        private void rbtnNoUnlockOver_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnNoUnlockOver.Checked )
            {
                rbtnAll.Checked = false;
                rbtnUnlockOver.Checked = false;
            }
        }

        private void rbtnUnlockOver_CheckedChanged(object sender, EventArgs e)
        {
            if(rbtnUnlockOver.Checked)
            {
                rbtnAll.Checked = false;
                rbtnNoUnlockOver.Checked = false;
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            int iLockMode = 0;          //锁定模式：1、指定工单；2、指定批号；3、指定芯片
            string strKey = string.Empty;
            if (rbtnGDNo.Checked)
            {
                if (txtGDNo.Text.Trim().Equals(string.Empty))
                {
                    GeneralTools.ShowInfoMsg("请先输入要锁定的工单号！");
                    txtGDNo.Focus();
                    return;
                }
                iLockMode = 1;
                strKey = txtGDNo.Text.Trim();
            }
            if (rbtnSN.Checked)
            {
                if (txtSN.Text.Trim().Equals(string.Empty))
                {
                    GeneralTools.ShowInfoMsg("请先输入要锁定的批号！");
                    txtSN.Focus();
                    return;
                }
                iLockMode = 2;
                strKey = txtSN.Text.Trim();
            }
            if (rbtnChipNo.Checked)
            {
                if (txtChipNo.Text.Trim().Equals(string.Empty))
                {
                    GeneralTools.ShowInfoMsg("请先输入要锁定的芯片号！");
                    txtChipNo.Focus();
                    return;
                }
                iLockMode = 3;
                strKey = txtChipNo.Text.Trim();
            }
            DataTable tmpdt = BuildLockList(iLockMode, strKey);
            if (tmpdt != null && tmpdt.Rows.Count > 0)
            {
                frmLock newFrmOperate = new frmLock(iLockMode,ui.UserName , strKey, tmpdt);
                newFrmOperate.ShowDialog();
            }
            else
            {
                GeneralTools.ShowInfoMsg("没有可处理的数据！");
            }
        }

        /// <summary>
        /// 建立锁定清单
        /// </summary>
        /// <param name="iLockMode">锁定模式：1、指定工单；2、指定批号；3、指定芯片</param>
        /// <param name="strKey">关键词</param>
        /// <returns>锁定清单</returns>
        private DataTable BuildLockList(int iLockMode, string strKey)
        {
            string strSelectString = "select gxjy000 工序结余ID,gxjy001 工单号,gxjy004 型号规格,gxjy007 工序,gxjy008 结余数量,gxjy016 批号,chipsn 芯片号,gxjy013 淘汰率,gxjy014 筛选数量,gxjy011 货架号,gxjy015 备注 from gxjy_table left join gd_table on gd001=gxjy001 where (Locked=0 or Locked is null) and gxjy007<>'报废仓' and gxjy008>0";
            switch (iLockMode)
            {
                case 1:     //指定工单
                    strSelectString += " and gxjy001='" + strKey + "'";
                    break;
                case 2:     //指定批号
                    strSelectString += " and (gxjy016='" + strKey + "' or gxjy016 like '"+strKey +"/%')";
                    break;
                case 3:     //指定芯片
                    strSelectString += " and chipsn='" + strKey + "'";
                    break;
            }
            return sqt_TP.GetData(strSelectString);
        }

        /// <summary>
        /// 建立解锁清单
        /// </summary>
        /// <returns>解锁清单</returns>
        private DataTable BuildUnlockList(string strMainID)
        {
            string strSelectString = "select ID 序号,GxjyID 工序结余ID,gxjy004 型号规格,gxjy007 工序,OriginalGxjy 原结余数量,OriginalBz 原备注,GDNo 工单号,SN 批号,ChipSN 芯片号 from LockDetailLog left join TPAMain .dbo.gxjy_table on gxjy000=GxjyID where MainID='"+strMainID +"' and Unlock=0";
            return sqt_DB.GetData(strSelectString);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strSelectString = "select top "+nudMaxRecordCount .Value .ToString ()+" ID 锁定ID,LockMode 锁定模式,LockKey 关键词,LockReason 锁定原因,LockOperator 锁定人,LockOperateTime  锁定时间,(select COUNT(*) from LockDetailLog where MainID =LockMainLog.ID) 全部记录数量,(select COUNT(*) from LockDetailLog where MainID =LockMainLog.ID and Unlock =0) 未解锁记录数量  from LockMainLog";
            if (rbtnGDNo .Checked )
            {
                if (!txtGDNo.Text.Trim().Equals(string.Empty))
                {
                    strSelectString += " where ID in (select MainID from LockDetailLog where GDNo='" + txtGDNo.Text.Trim() + "'";
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=0";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=1";
                    }
                    strSelectString += ")";
                }
                else
                {
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=0)";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=1)";
                    }
                }
            }
            if (rbtnSN .Checked)
            {
                if (!txtSN.Text.Trim().Equals(string.Empty))
                {
                    strSelectString += " where ID in (select MainID from LockDetailLog where SN like '" + txtSN.Text.Trim() + "%'";
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=0";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=1";
                    }
                    strSelectString += ")";
                }
                else
                {
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=0)";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=1)";
                    }
                }
            }
            if (rbtnChipNo .Checked)
            {
                if (!txtChipNo.Text.Trim().Equals(string.Empty))
                {
                    strSelectString += " where ID in (select MainID from LockDetailLog where ChipSN like '" + txtChipNo.Text.Trim() + "'";
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=0";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " and Unlock=1";
                    }
                    strSelectString += ")";
                }
                else
                {
                    if (rbtnNoUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=0)";
                    }
                    if (rbtnUnlockOver.Checked)
                    {
                        strSelectString += " where ID in (select MainID from LockDetailLog where Unlock=1)";
                    }
                }
            }
            dgv1.DataSource = sqt_DB.GetData(strSelectString);
            dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1 .CurrentCell !=null )
            {
                string strSelectString = "select ID 序号,GxjyID 工序结余ID,gxjy004 型号规格,gxjy007 工序,OriginalGxjy 原结余数量,OriginalBz 原备注,GDNo 工单号,SN 批号,ChipSN 芯片号,Unlock 是否解锁,UnlockOperator 解锁人,UnlockReason 解锁原因,UnlockTime 解锁时间 from LockDetailLog left join TPAMain .dbo.gxjy_table on gxjy000=GxjyID where MainID='"+dgv1 .CurrentRow .Cells ["锁定ID"].Value .ToString ()+"'";
                dgv2.DataSource = sqt_DB.GetData(strSelectString);
                dgv2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
                if (GeneralTools.GetInt(dgv1.CurrentRow.Cells["未解锁记录数量"].Value , 0)>0)
                {
                    btnUnlock.Enabled = true;
                }
                else
                {
                    btnUnlock.Enabled = false;
                }
            }
            else
            {
                dgv2.DataSource = null;
                btnUnlock.Enabled = false;
            }
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentCell != null)
            {
                DataTable tmpdt =BuildUnlockList  (dgv1 .CurrentRow.Cells ["锁定ID"].Value .ToString ());
                if (tmpdt != null && tmpdt.Rows.Count > 0)
                {
                    frmUnlock newFrmOperate = new frmUnlock(ui.UserName, tmpdt);
                    newFrmOperate.ShowDialog();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("没有可处理的数据！");
                }
            }
        }
    }
}
